﻿using System;
using CalcSharp.Ui;
using WeifenLuo.WinFormsUI.Docking;

namespace CalcSharp.MDIUI.ToolBars
{
    public enum mMapSegment : byte
    {
        mFloat = 0,
        mMatrix,
        mCplx,
        mSet,
        mFunc,
        mString,
        mAll
    }

    public partial class MemMap : DockContent
    {
        public MemMap()
        {
            InitializeComponent();
        }

        public void UpdateMemMap(mMapSegment which)
        {
            int n;

            if (which == mMapSegment.mAll || which == mMapSegment.mFloat)
            {
                floatView.Rows.Clear();
                foreach (string key in Program.loaded.Mem.keys)
                {
                    n = floatView.Rows.Add();
                    floatView.Rows[n].Cells[0].Value = key;
                    floatView.Rows[n].Cells[1].Value = Program.loaded.Mem[key];
                }
            }
            if (which == mMapSegment.mAll || which == mMapSegment.mMatrix)
            {
                mtrxview.Rows.Clear();
                foreach (string key in Program.loaded.Mtrx_mem.keys)
                {
                    n = mtrxview.Rows.Add();
                    mtrxview.Rows[n].Cells[0].Value = key;
                    mtrxview.Rows[n].Cells[1].Value = ConsoleUI.DFileSize(Program.loaded.Mtrx_mem[key].Columns * Program.loaded.Mtrx_mem[key].Rows * (byte)Program.loaded.Mtrx_mem.StorageType);
                }
            }
            if (which == mMapSegment.mAll || which == mMapSegment.mCplx)
            {
                cplxview.Rows.Clear();
                foreach (string key in Program.loaded.CPLX_mem.keys)
                {
                    n = cplxview.Rows.Add();
                    cplxview.Rows[n].Cells[0].Value = key;
                    cplxview.Rows[n].Cells[1].Value = Program.loaded.CPLX_mem[key];
                }
            }
            if (which == mMapSegment.mAll || which == mMapSegment.mSet)
            {
                SetView.Rows.Clear();
                foreach (string key in Program.loaded.Set_mem.keys)
                {
                    n = SetView.Rows.Add();
                    SetView.Rows[n].Cells[0].Value = key;
                    SetView.Rows[n].Cells[1].Value = Program.loaded.Set_mem[key].Count;
                }
            }
            if (which == mMapSegment.mAll || which == mMapSegment.mFunc)
            {
                func_list.Items.Clear();
                foreach (string fnc in Program.functionmem.keys)
                {
                    func_list.Items.Add(fnc);
                }
            }
            if (which == mMapSegment.mAll || which == mMapSegment.mString)
            {
                StringView.Rows.Clear();
                foreach (string key in Program.loaded.String_mem.keys)
                {
                    n = StringView.Rows.Add();
                    StringView.Rows[n].Cells[0].Value = key;
                    StringView.Rows[n].Cells[1].Value = Program.loaded.String_mem[key];
                }
            }
        }

        private void mem_map_Load(object sender, EventArgs e)
        {
            this.UpdateMemMap(mMapSegment.mAll);
        }

        private void BtnRefresh_Click(object sender, EventArgs e)
        {
            this.UpdateMemMap(mMapSegment.mAll);
        }
    }
}